<template>
  <el-dialog
    :visible.sync="visible"
    :close-on-click-modal="false"
    :close-on-press-escape="false"
    @closed="closedDialog"
    :append-to-body="true"
    :title="$t('SYS.editPassword')">
    <!-- 修改密码 -->
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="submitForm" :label-width="$i18n.locale=='zh_cn'? '120px':'160px'">
      <el-form-item :label="$t('SYS.userName')">
        <span>{{ $store.state.userInfo.username }}</span>
      </el-form-item>
      <el-form-item prop="password" :label="$t('SYS.oldPassword')">
        <el-input v-model="dataForm.password" type="password" :placeholder="$t('SYS.pleaseInput')+$t('SYS.oldPassword')"></el-input>
      </el-form-item>
      <el-form-item prop="newPassword" :label="$t('SYS.newPassword')">
        <el-input v-model="dataForm.newPassword" type="password" :placeholder="$t('SYS.pleaseInput')+$t('SYS.newPassword')"></el-input>
      </el-form-item>
      <el-form-item prop="confirmPassword" :label="$t('SYS.newPassword')">
        <el-input v-model="dataForm.confirmPassword" type="password" :placeholder="$t('SYS.passwordAgain')"></el-input>
      </el-form-item>
    </el-form>
    <template slot="footer">
      <el-button @click="visible = false">{{$t('SYS.close')}}</el-button>
      <el-button type="primary" @click="submitForm">{{$t("SYS.check")}}</el-button>
    </template>
  </el-dialog>
</template>

<script>
import { clearLoginInfo } from '@/utils'
export default {
  data(){
    var validateConfirmPassword = (rule, value, callback) => {
        if (this.dataForm.newPassword !== value) {
          return callback(new Error(this.$t('SYS.passWordError')))
        }
        callback()
      }
    return {
      visible:false,
      dataRule:{
        password: [
          { required: true, message: this.$t('SYS.pleaseInput')+this.$t('SYS.oldPassword'), trigger: 'blur' }
        ],
        newPassword: [
          { required: true, message: this.$t('SYS.pleaseInput')+this.$t('SYS.newPassword'), trigger: 'blur' }
        ],
        confirmPassword: [
          { required: true, message: this.$t('SYS.passwordAgain'), trigger: 'blur' },
          { validator: validateConfirmPassword, trigger: 'blur' }
        ]
      },
      dataForm:{
        newPassword:"",
        password:""
      }
    }
  },
  methods:{
    // 控制dialog显示
    init(){
      this.visible = true
    },
    submitForm(){
      this.$refs['dataForm'].validate((valid) => {
        if (!valid) {
          return false
        }
        this.$http.put(this.$baseUrl.sysUserPassword, this.dataForm).then(({data: res})=>{
          if (res.code !== 0) {
            return this.$message.error(res.msg)
          }
          this.$message.success(this.$t('request.editSuccess'))
          this.$store.commit('clearLoginInfo')
          this.$router.replace({path:'/login'})
        }).catch(()=>{
          this.$message.error(this.$t('request.serverError'))
        })
      })
    },
    closedDialog(){
      this.$emit('closedDialog')
    }
  }
}
</script>

<style>

</style>